<template>
  <div>
    <el-upload
      ref="upload"
      multiple
      :auto-upload="false"

      :accept="accept"
      :file-list="fileList"

      action=""
      :before-upload="beforeUpload"
      :http-request="customUpload"

      :limit="limit"

      :on-remove="handleRemove"
      :on-preview="handlePreview"
      :on-exceed="handleExceed"
    >
      <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
      <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button>
      <div slot="tip" class="el-upload__tip">{{ tip }}</div>
    </el-upload>

    <el-dialog v-if="dialogVisible" :visible.sync="dialogVisible">
      <el-row :gutter="12">
        <el-col v-for="(file,index) in fileList" :key="index" :span="3">
          <el-link type="primary" target="_blank" width="100%" :href="file.url" :alt="file.name">
            {{ file.name }}
          </el-link>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>

import { upload } from '@/api/file/upload'

export default {
  name: 'MultiUpload',
  props: {
    // 文件属性数组
    value: {
      type: Array,
      default: () => []
    },
    // 最大上传文件数量
    limit: {
      type: Number,
      default: 6
    },
    folder: {
      type: String,
      default: 'common'
    },
    // 文件大小限制，默认5M
    sizeLimit: {
      type: Number,
      default: 5
    },
    accept: {
      type: String,
      default: '.png,.jpg,.jpeg'
    },
    tip: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      dialogVisible: false,
      dialogImageUrl: null
    }
  },
  computed: {
    fileList() {
      const fileList = []

      if (this.value) {
        for (let i = 0; i < this.value.length; i++) {
          fileList.push({
            name: this.value[i].name,
            url: this.value[i].url
          })
        }
      }

      return fileList
    }
  },
  methods: {
    emitInput(fileList) {
      this.$emit('input', fileList)
    },
    handleRemove(file, fileList) {
      this.emitInput(fileList)
    },
    handlePreview(file) {
      this.dialogVisible = true
    },
    beforeUpload(file) {
      // 校验文件大小
      const sizeLimit = this.sizeLimit
      const isLtSizeLimit = file.size / 1024 / 1024 < sizeLimit
      if (!isLtSizeLimit) {
        this.$message.error(`上传图片大小不能超过 ${sizeLimit}MB!`)
      }

      return isLtSizeLimit
    },
    handleUploadSuccess(res) {
      // element上传多个文件，只有一次回调,如果在这拼接fileList，会存在一些问题，
      // 所以和SingleUpload不一样的时，最好重写上传方法，在请求里面里面进行拼接
      this.fileList.push({ name: res.data.filename, url: res.data.absoluteUrl })
      this.emitInput(this.fileList)
    },
    handleExceed(files, fileList) {
      this.$message({
        message: '最多只能上传' + this.limit + '张文件',
        type: 'warning',
        duration: 1000
      })
    },
    submitUpload() {
      this.$refs.upload.submit()
    },
    customUpload(params) {
      const { file, filename } = params

      const form = new FormData()
      form.append(filename, file)
      form.append('folder', this.folder)

      upload(form).then(res => {
        this.handleUploadSuccess(res)
      })
    }
  }
}
</script>
<style>

</style>

